package com.example.administrator.uiautomator;

import android.content.Context;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.UiWatcher;
import android.support.test.uiautomator.Until;

import com.example.administrator.uiautomator.utils.Finder;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Created by Administrator on 2017/7/4.
 */


@RunWith(AndroidJUnit4.class)
///说明用androidJunit4
public class BasicTest {
    //声明一个 UiDevice，因为所有的UIAutomat都通过这个device来实现；
    private UiDevice device;
    // 要测试应用的包名
    private static String PACKAGE_NAME = "com.netease.newsreader.activity";
    private Finder finder;
    public Helper helper;

    @Before
    public void setUp() throws Exception {
        //实例化 UiDevice
        device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        //启动应用
        //获取上下文
        Context context = InstrumentationRegistry.getContext();
        //通过将 包名 传给包管理器 获取 启动的 intent
        final Intent intent = context.getPackageManager().getLaunchIntentForPackage(PACKAGE_NAME);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        //启动应用
        context.startActivity(intent);
        //等待应用 启动
        device.wait(Until.hasObject(By.pkg(PACKAGE_NAME).depth(0)), 1000);

        device.registerWatcher("mywatcher", new UiWatcher() {
            @Override
            public boolean checkForCondition() {
                boolean result = false;
                UiObject2 allowBtn = device.findObject(By.res("com.android.packageinstaller:id/permission_allow_button"));
                if (allowBtn != null){
                    allowBtn.click();
                    result = true;
                }
                UiObject2 cancleUpdate = device.findObject(By.res("com.netease.newsreader.activity:id/n6"));
                if (cancleUpdate != null){
                    cancleUpdate.click();
                    result = true;
                }
                return result;
            }
        });

        finder = new Finder(device);
        helper = new Helper(finder);

    }

    @Test
    public void name() throws Exception {

    }

    @After
    public void tearDown() throws Exception {


    }
}
